package cn.edu.scau.cmi.hekaimin.Hibernate.controller;

import javafx.fxml.FXML;

import javafx.scene.text.Text;

import javafx.scene.control.Button;

import java.util.List;
import java.util.Random;

import cn.edu.scau.cmi.hekaimin.Hibernate.dao.StudentDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Student;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.ComboBox;

public class StudentFXController {
	@FXML
	private Text text_stu;
	@FXML
	private Text text_tea;
	@FXML
	private Button btn_ch;
	@FXML
	private Button btn_re;
	@FXML
	private ComboBox comboBox;
	
	private ObservableList<String> teachers = FXCollections.observableArrayList();
	private String last_teacher = null;
	private Student student = null;
	public void init() {
		StudentDao stu = new StudentDao();   //准备随机读取学生
		TeacherDao tea = new TeacherDao();   //便于显示导师下拉列表
		
		List<Student> students = stu.getAll();
		Random random = new Random();
		int n = random.nextInt(students.size());  //随机获取学生
		student = students.get(n);
		text_stu.setText("欢迎学生: " + student.getS_name() + " 登录!");
		
		List<Teacher> teachers2 = tea.getAll();
		for(Teacher e : teachers2) { 
			teachers.add(e.getT_name());        //填充下拉列表
		}
		comboBox.setItems(teachers);
		
		if(student.getTeacher() != null) {
			btn_ch.setDisable(true);
			btn_re.setDisable(false);
			text_tea.setText("您的导师是: " + student.getTeacher().getT_name());
			last_teacher = student.getTeacher().getT_name();      //记录student已经选择的导师,便于修改下拉列表
			for(String e:teachers) {
				if(e.equals(student.getTeacher().getT_name())) {
					teachers.remove(e);
					break;
				}
			}
		}else {
			btn_ch.setDisable(false);    //当学生还未选择指导老师时
			btn_re.setDisable(true);
		}
	}
	@FXML
	public void chooseTea(ActionEvent event) {
		StudentDao stu = new StudentDao();
		TeacherDao tea = new TeacherDao();
		List<Teacher> teachers2 = tea.getAll();
		for(Teacher e:teachers2) {
			if(e.getT_name().equals((String)comboBox.getValue())) {
				student.setTeacher(e);
				last_teacher = (String)comboBox.getValue(); //记录新值
				stu.update(student);
				break;
			}
		}	
		        for(String e: teachers) {
		        	if(e.equals(last_teacher)) {
		        		teachers.remove(e);
		        		break;
		        	}
		        }
		        text_tea.setText("您的导师是: " + last_teacher);
				btn_ch.setDisable(true);
				btn_re.setDisable(false);
				comboBox.getSelectionModel().select(-1);
	}
	// Event Listener on Button[#btn_re].onAction
	@FXML
	public void reviseChoose(ActionEvent event) {
		StudentDao stu = new StudentDao();
		TeacherDao tea = new TeacherDao();
		List<Teacher> teachers2 = tea.getAll();
		Teacher newChoose = null;
		String newteacher = (String)comboBox.getValue();
		for(Teacher e : teachers2) {
			if(e.getT_name().equals((String)comboBox.getValue())) {
				newChoose = e;
				break;
			}
		}
		student.setTeacher(newChoose);
		stu.update(student);
		
		for(String e:teachers) {
			if(e.equals(newteacher)){
				teachers.remove(e);
				teachers.add(last_teacher);
				text_tea.setText("您的导师是: " + e );  //更新 text和 下拉列表
				last_teacher = e;
				comboBox.getSelectionModel().select(-1); //恢复下拉列表未选择状态
				break;
			}
		}
	}
}
